{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c463c1d-80a7-4cec-b96c-91da8882b7f6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "class MyRange:\n",
    "    def __init__(self, start, end):\n",
    "        self.start = start\n",
    "        self.end = end\n",
    "\n",
    "    def __iter__(self):\n",
    "        # 返回迭代器对象本身\n",
    "        return self\n",
    "\n",
    "    def __next__(self):\n",
    "        # 如果当前值小于结束值，返回当前值并更新状态\n",
    "        if self.start < self.end:\n",
    "            current = self.start\n",
    "            self.start += 1\n",
    "            return current\n",
    "        else:\n",
    "            # 如果没有更多值，抛出 StopIteration 异常\n",
    "            raise StopIteration\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c6cfff25-2c24-49bf-938b-d70491f00878",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用自制迭代器\n",
    "my_range = MyRange(1, 5)\n",
    "for num in my_range:\n",
    "    print(num)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Py(Python note)",
   "language": "python",
   "name": "pynote"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
